#coding=utf-8
from django.shortcuts import render_to_response,RequestContext
from django.http import HttpResponse,HttpResponseRedirect,response
# from django.template import RequestContext
from django import forms
from models import YongHu,YiSheng,YaoPin


# Create your views here.
#表单

    
class UserForm(forms.Form): 
    username = forms.CharField(label='用户名',max_length=50)
    password = forms.CharField(label='密码',widget=forms.PasswordInput())
   

class UserRegisterForm(forms.Form):
    username = forms.CharField(label='用户名：',max_length=50)
    password = forms.CharField(label='密码：',max_length=50)


#注册
def regist(request):
    if request.method == 'POST':
        urf = UserRegisterForm(request.POST)
        if urf.is_valid():
            #获得表单数据
            username = urf.cleaned_data['username']
            password = urf.cleaned_data['password']
            
            YongHuZhuCe = YongHu.objects.filter(YongHuMing=username)
            if YongHuZhuCe:
                warning = "用户已存在！"
                return render_to_response('regist.html',{'warning':warning})   
            else:             
                #添加到数据库
                YongHu.objects.create(YongHuMing=username,MiMa=password)
                return HttpResponseRedirect('/zhensuoguanli/SRegist/')
    else:
        urf = UserRegisterForm()
    return render_to_response('regist.html',{'urf':urf}, context_instance=RequestContext(request))

#登录
def login(request):
    if request.method == 'POST':
        uf = UserForm(request.POST)
        if uf.is_valid():
            #获取表单用户密码
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            #获取表单数据与数据库数进行比较
            YongHuDengLu = YongHu.objects.filter(YongHuMing=username,MiMa=password)
            YiShengDenglu = YiSheng.objects.filter(YongHuMing=username,MiMa=password)
            if YongHuDengLu:
                #比较患者用户名成功，跳转index
                response = HttpResponseRedirect('/zhensuoguanli/Cindex/')
                #将username写入浏览器cookies，失败时间为3600
                response.set_cookie('username',username,3600)
                return response
            elif YiShengDenglu:
                #比较医生用户名成功，跳转base
                response = HttpResponseRedirect('/zhensuoguanli/Dindex/')
                #将username写入浏览器cookies，失败时间为3600
                response.set_cookie('username',username,3600)
                return response
            else:
                #比较失败，还在login
                warning = "密码或用户名错误"
                return render_to_response('login.html',{'warning':warning})        
#                 return HttpResponseRedirect('/zhensuoguanli/login/')
    else:
        uf=UserForm
    return render_to_response('login.html',{'uf':uf},context_instance=RequestContext(request))

#登陆成功
def index(request):
    username = request.COOKIES.get('username')
    return render_to_response('index.html' ,{'username':username})



#医生登录成功
def Dindex(request):
    YongHuForm = YongHu.objects.order_by('YongHuMing')[:];
    YaoPinForm = YaoPin.objects.order_by('TongYongMing')[:];
    username = request.COOKIES.get('username')
    return render_to_response('Dindex.html',
                              locals(),
                              context_instance = RequestContext(request)
                                )
    
def Cindex(request):
    GeRenXinXiForm = YongHu.objects.order_by('YongHuMing')    
    username = request.COOKIES.get('username')
    return render_to_response('Cindex.html',
                              locals(),
                              context_instance = RequestContext(request)
                                )
#退出
def logout(request):
    response = render_to_response('logout.html')
    #清楚cookie数据
    response.delete_cookie('username')
    return response
#注册成功
def SRegist(requet):
    return render_to_response('SRegist.html')
